/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Welcome.java
 *
 * Created on 2008-12-14, 19:25:12
 */
package impss;

import java.sql.*;
import javax.swing.*;

/**
 *
 * @author Mikehood
 */
public class Welcome extends javax.swing.JFrame {

    int sort, id;
    boolean costPrice = true;
    String Nickname, Password, Name, price;
    DBAdapter adapter;
    private ResultSet rs;
    String url = "jdbc:mysql://localhost:3306/impss";
    String name = "root";
    String password = "password12345";

    public void setText() {
        try {
            String sql = "select * from user where (ID = '" + id + "');";
            rs = adapter.executeQuery(sql);
            boolean Records = rs.next();
            if (!Records) {
                JOptionPane.showMessageDialog(this, "error");
                return;
            } else {
                Nickname = rs.getString("nickname");
                jTextField1.setText(Nickname);
            }
            switch (sort) {
                case 0: //手机公司
                    jTextField4.setText("山寨手机公司");
                    jTextField5.setText("无");
                    costPrice = false;
                    break;
                case 2: //配件厂
                    sql = "select * from af where (afID = '" + id + "')";
                    rs = adapter.executeQuery(sql);
                    Records = rs.next();
                    if (!Records) {
                        JOptionPane.showMessageDialog(this, "error");
                        return;
                    } else {
                        Name = rs.getString("afName");
                        jTextField4.setText(Name);
                    }
                    jTextField5.setText("无");
                    costPrice = false;
                    break;
                case 4: //消费者
                    sql = "select * from customer where (customerID = '" + id + "')";
                    rs = adapter.executeQuery(sql);
                    Records = rs.next();
                    if (!Records) {
                        JOptionPane.showMessageDialog(this, "error");
                        return;
                    } else {
                        Name = rs.getString("customerName");
                        jTextField4.setText(Name);
                    }
                    jTextField5.setText("无");
                    costPrice = false;
                    break;
                case 5: //设计师
                    sql = "select * from designer where (designerID = '" + id + "')";
                    rs = adapter.executeQuery(sql);
                    Records = rs.next();
                    if (!Records) {
                        JOptionPane.showMessageDialog(this, "error");
                        return;
                    } else {
                        Name = rs.getString("designerName");
                        jTextField4.setText(Name);
                        price = rs.getString("DesignerPrice");
                        jTextField5.setText(price);
                    }
                    break;
                case 6: //快递公司
                    sql = "select * from express where (expressID = '" + id + "')";
                    rs = adapter.executeQuery(sql);
                    Records = rs.next();
                    if (!Records) {
                        JOptionPane.showMessageDialog(this, "error");
                        return;
                    } else {
                        Name = rs.getString("expressName");
                        jTextField4.setText(Name);
                        price = rs.getString("expressPrice");
                        jTextField5.setText(price);
                    }
                    break;
                case 7: //加工厂
                    sql = "select * from pf where (pfID = '" + id + "')";
                    rs = adapter.executeQuery(sql);
                    Records = rs.next();
                    if (!Records) {
                        JOptionPane.showMessageDialog(this, "error");
                        return;
                    } else {
                        Name = rs.getString("pfName");
                        jTextField4.setText(Name);
                        price = rs.getString("pfPrice");
                        jTextField5.setText(price);
                    }
                    break;
                default:
                    break;
            }
        } catch (SQLException sqlex) {
        }
    }

    /** Creates new form Welcome */
    public Welcome() {
        initComponents();
        adapter = new DBAdapter();
    }

    public void setSort(int s) {
        sort = s;
    }

    public void setID(int i) {
        id = i;
    }

    //修改注册信息
    private void saveChange() {
        String nk = jTextField1.getText();
        String psw = jTextField2.getText();
        String pwd = jTextField3.getText();
        String nm = jTextField4.getText();
        String pr = jTextField5.getText();
        if (nk.equals("")) {
            JOptionPane.showMessageDialog(this, "昵称不能为空！");
            return;
        }
        if (nm.equals("")) {
            JOptionPane.showMessageDialog(this, "名称不能为空！");
            return;
        }
        if (costPrice && pr.equals("")) {
            JOptionPane.showMessageDialog(this, "价格不能为空！");
            return;
        }
        if (psw.equals("")) {
            JOptionPane.showMessageDialog(this, "密码不能为空！");
            return;
        }
        if (!psw.equals(pwd)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致！");
            return;
        }
        String sql = "update user set nickname='" + nk + "',password='" + psw + "' where (ID = '" + id + "')";
        adapter.execute(sql);
        switch (sort) {
            case 0:
                break;
            case 2:
                sql = "update af set afName='" + nm + "' where(afID='" + id + "');";
                break;
            case 4:
                sql = "update customer set customerName='" + nm + "' where(customerID='" + id + "');";
                break;
            case 5:
                sql = "update designer set designerName='" + nm + "',designerPrice='" + pr + "' where(designerID='" + id + "');";
                break;
            case 6:
                sql = "update express set expressName='" + nm + "',expressPrice='" + pr + "' where(expressID='" + id + "');";
                break;
            case 7:
                sql = "update pf set pfName='" + nm + "',pfPrice='" + pr + "' where(pfID='" + id + "');";
                break;
            default:
                break;
        }
        adapter.execute(sql);
        JOptionPane.showMessageDialog(this, "修改成功！");
    }

    private void enter() {
        switch (sort) {
            case 0:
                PhoneCompanyArea lao = new PhoneCompanyArea();
                lao.setVisible(true);
                break;
            case 2:
                AFArea pei = new AFArea();
                pei.setID(id);
                pei.setTable();
                pei.setVisible(true);
                break;
            case 4:
                BuyArea din = new BuyArea();
                din.setID(id);
                din.setVisible(true);
                break;
            case 5:
                DesignArea she = new DesignArea();
                she.setID(id);
                she.setDesignTable();
                she.setVisible(true);
                break;
            case 6:
                ExpressArea son = new ExpressArea();
                son.setID(id);
                son.setTable();
                son.setVisible(true);
                break;
            case 7:
                PFArea jia = new PFArea();
                jia.setID(id);
                jia.setProTable();
                jia.setVisible(true);
                break;
            default:
                break;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("欢迎");

        jLabel1.setText("修改信息：");

        jLabel2.setText("昵称：");

        jLabel3.setText("密码：");

        jLabel4.setText("确认密码：");

        jLabel5.setText("姓名（或厂名）：");

        jButton1.setText("保存修改");
        jButton1.setMaximumSize(new java.awt.Dimension(93, 23));
        jButton1.setMinimumSize(new java.awt.Dimension(93, 23));
        jButton1.setPreferredSize(new java.awt.Dimension(93, 23));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("进入工作区");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText("收费：");

        jButton3.setText("返回主界面");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel2)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addComponent(jButton3)))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2)
                            .addComponent(jTextField3)
                            .addComponent(jTextField4)
                            .addComponent(jTextField1)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        saveChange();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        enter();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Login login = new Login();
        login.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Welcome().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
